Skip to content

P2P Bluetooth peer discovery changes#456

Merged
Aniket392 merged 11 commits intomasterfrom
wip/p2p_bt
Mar 20, 2026
Merged

P2P Bluetooth peer discovery changes#456
Aniket392 merged 11 commits intomasterfrom
wip/p2p_bt

Conversation

@Aniket392
Copy link
Copy Markdown
Contributor

  • Adding c4Peerdiscovery implementation, implementing all methods and connecting it to java
  • BT connections discovery logic from both native as well as java side
  • Writing helper class like MetadataHelper, to convert java map object
  • Adding native implementation to BluetoothPeer
  • Updating native_c4multipeerreplicator to take protocol parameter

@Aniket392 Aniket392 requested a review from pasin February 23, 2026 06:37
xmlns:tools="http://schemas.android.com/tools"
>

<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this mean to users who don't use MultipeerReplicator with Bluetooth?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If anyone do not use MultipeerReplicator with Bluetooth, this permission has no effect.

C4String protocols[] = {kPeerSyncProtocol_DNS_SD};
params.protocols = protocols;
params.protocolsCount = sizeof(protocols) / sizeof(protocols[0]);
params.protocols = kPeerSyncProtocol_BluetoothLE;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to be passed as an array of transport values.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added
params.protocols = kPeerSyncProtocol_DNS_SD | kPeerSyncProtocol_BluetoothLE;

m_C4PeerDiscoveryProvider_startBrowsing,
_contextToken);


Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra empty line.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed extra spaces

}



Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra lines.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed extra lines

auto peer = provider->discovery().peerWithID("peerIdObj");
if (peer) {
// Notify about the incoming connection
bool accepted = provider->notifyIncomConnection(peer.get(), socket);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

notifyIncomConnection -> there is a typo in this function name.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently removed this
Will update with Socketfactory

return setObj;
}

static C4PeerSyncProtocols toC4PeerSyncProtocols(JNIEnv* env, jobject enumSetTransports) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have looked where the other components implement something similar to this (e.g. database flags), the enums are implemented on the Java side in C4Constants class with the enum values copied from LiteCore.

Let's follow the same pattern. Here is an example :

https://github.com/couchbase/couchbase-lite-java-common/blob/master/common/main/java/com/couchbase/lite/internal/core/C4Database.java#L210

https://github.com/couchbase/couchbase-lite-java-common/blob/master/common/main/java/com/couchbase/lite/internal/core/C4Constants.java#L112-L119

Then the raw enum value can be passed between Java and JNI directly.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for the transport enum above.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For toC4PeerSyncProtocols, we can use the same pattern as you shared
But when we are converting from litecore flag to Java object, we need to have something like toJavaTransportSet

It is similar how we have

jobject toJavaReplStatus(JNIEnv *env, C4ReplicatorStatus status) {
return env->NewObject(
cls_C4ReplStatus,
m_C4ReplStatus_init,
(jint) status.level,
(jlong) status.progress.unitsCompleted,
(jlong) status.progress.unitsTotal,
(jlong) status.progress.documentCount,
(jint) status.error.domain,
(jint) status.error.code,
(jint) status.error.internal_info);
}

Or do you suggest something else?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed toC4PeerSyncProtocols and other transport conversion method

std::string peerId = JstringToUTF8(env, jPeerId);
if (peerId.empty()) { return 0; }

auto* peer = new C4Peer(provider, peerId);
Copy link
Copy Markdown
Collaborator

@pasin pasin Mar 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think make_retained should be used like this to get the refcount +1.

make_retained<C4Peer>(this, "null::" + id, "").detach()

However, when does this createC4Peer is used? If it's used by adding new peer. I think it's better to have the JNI's addPeer() method to create the C4Peer object by itself, add it to the C4DiscoveryProvider, then return the detached C4Peer object back to Java to retain if needed. By doing it that way, there will be only one JNI call.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated as shared

@Aniket392 Aniket392 requested a review from pasin March 6, 2026 11:11
@Aniket392 Aniket392 merged commit 994fee7 into master Mar 20, 2026
@Aniket392 Aniket392 deleted the wip/p2p_bt branch March 20, 2026 10:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants